﻿using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using BuildServer.Models;

namespace BuildServer.Controllers
{
    [Authorize]
    public class BuildController : Controller
    {
        private BuildServerDbContext db = new BuildServerDbContext();

        //
        // GET: /Build/
        public ActionResult Index()
        {
            var buildruns = db.BuildRuns.Include(b => b.BuildDefinition).OrderByDescending(x => x.Scheduled);
            return View(buildruns.ToList());
        }

        //
        // GET: /Build/Details/5
        public ActionResult Details(int id = 0)
        {
            BuildRun buildrun = db.BuildRuns.Find(id);
            if (buildrun == null)
            {
                return HttpNotFound();
            }
            return View(buildrun);
        }

        //
        // GET: /Build/Create
        public ActionResult Create()
        {
            ViewBag.BuildDefinitionId = new SelectList(db.BuildDefinitions, "Id", "Name");
            return View();
        }

        //
        // POST: /Build/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(BuildRun buildrun)
        {
            if (ModelState.IsValid)
            {
                db.BuildRuns.Add(buildrun);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BuildDefinitionId = new SelectList(db.BuildDefinitions, "Id", "Name", buildrun.BuildDefinitionId);
            return View(buildrun);
        }

        //
        // GET: /Build/Edit/5
        public ActionResult Edit(int id = 0)
        {
            BuildRun buildrun = db.BuildRuns.Find(id);
            if (buildrun == null)
            {
                return HttpNotFound();
            }
            ViewBag.BuildDefinitionId = new SelectList(db.BuildDefinitions, "Id", "Name", buildrun.BuildDefinitionId);
            return View(buildrun);
        }

        //
        // POST: /Build/Edit/5
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(BuildRun buildrun)
        {
            if (ModelState.IsValid)
            {
                db.Entry(buildrun).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BuildDefinitionId = new SelectList(db.BuildDefinitions, "Id", "Name", buildrun.BuildDefinitionId);
            return View(buildrun);
        }

        //
        // GET: /Build/Delete/5
        public ActionResult Delete(int id = 0)
        {
            BuildRun buildrun = db.BuildRuns.Find(id);
            if (buildrun == null)
            {
                return HttpNotFound();
            }
            return View(buildrun);
        }

        //
        // POST: /Build/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            BuildRun buildrun = db.BuildRuns.Find(id);
            db.BuildRuns.Remove(buildrun);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult BuildLog(int id)
        {
            BuildRun buildrun = db.BuildRuns.Find(id);
            if (buildrun == null)
            {
                return HttpNotFound();
            }
            string filename = buildrun.BuildLog;
            return new FilePathResult(filename, "text/plain");
        }

        public ActionResult CompileLog(int id)
        {
            BuildRun buildrun = db.BuildRuns.Find(id);
            if (buildrun == null)
            {
                return HttpNotFound();
            }
       
            string filename = buildrun.CompileLog;
            return new FilePathResult(filename, "text/html");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}